<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>webSocketClient</title>
    <script type="text/javascript">
        var socket;
        if(!window.WebSocket){
            alert("浏览器不支持websocket");
        }
        function connect() {
            socket = new WebSocket("ws://localhost:8000/chat");
            socket.onmessage = function (event) {
                var rt = document.getElementById("responseText");
                rt.value = rt.value + "\n"+event.data;
            }
            socket.onopen=function (event) {
                var rt = document.getElementById("responseText");
                rt.value = "建立WebSocket连接....";
            }
            socket.onclose=function (event) {
                var rt = document.getElementById("responseText");
                rt.value = rt.value + "\n 连接已关闭";
            }
        }

        function send(message){
            if(!window.socket){
                alert("还没有建立连接。")
                return;
            }
            if(socket.readyState == WebSocket.OPEN){
                socket.send(message)
            }else{
                alert("没有建立连接");
            }
        }

        function disconnect() {
            if(window.socket){
                socket.close();
                socket = null;
            }
        }

    </script>
</head>
<body>
<form onsubmit="return false">
    <table>

    </table>
    <input type="button" value="上线" onclick="connect()">
    <input type="button" value="下线" onclick="disconnect()">
    <textarea name="message" style="height: 300px; width: 300px"></textarea>
    <input type="button" value="发送消息" onclick="send(this.form.message.value)">
    <textarea id="responseText" style="height: 300px; width: 300px"></textarea>
    <input type="button" value="清空内容" onclick="document.getElementById('responseText').value=''">
</form>
</body>
</html>